#include "EventReceiver.h"


EventReceiver::EventReceiver(GraphicEngine *Engine, DealProtocol *pDealer, LedDriver * myLed)
{
	AEngine = Engine;
	ProtDealer = pDealer;
	
	myStimuli = myLed;
	Xscale = 1.0f;
}

EventReceiver::~EventReceiver(void)
{
}


bool EventReceiver::OnEvent(const SEvent &event)
{
	BCIMessage KeyMessage;
	scene::ISceneNode*	mySceneNode;
	int Periods[4]={128,128,128,128};
	// KEY INPUT EVENTS
	if (event.EventType == irr::EET_KEY_INPUT_EVENT && !event.KeyInput.PressedDown)
	{
		switch (event.KeyInput.Key)
		{
		case irr::KEY_KEY_P:
			//Controlling the feedback bar to know if the protocol is already started
			
			if (ProtDealer->pProtocol->isRunning==false) 
			{
				cout<<"REQUEST STARTING PROTOCOL"<<endl;
				ProtDealer->pProtocol->RequestParams();
				Sleep(500);
				ProtDealer->pProtocol->SendStart();
			}
			break;
		case irr::KEY_KEY_F:
			if (ProtDealer->pProtocol->isRunning==false) 
			{
				cout<<"STARTING PROTOCOL"<<endl;
				/*if (ProtDealer->pProtocol->DoPlayMusic) 
				{
					ProtDealer->pProtocol->MainSound->ResetMusic();
					ProtDealer->pProtocol->MainSound->PlayMusic();
				}*/
				ProtDealer->StartProt();
			}
			break;
		case irr::KEY_KEY_O:
			//Controlling the feedback bar to know if the protocol is already stopped
			if (ProtDealer->pProtocol->isRunning==true)
			{
				mySceneNode = AEngine->smgr->getSceneNodeFromName(FB_LEFT);
				mySceneNode->setScale(core::vector3df(1.0f, 0.1f, 1.0f));
				mySceneNode->setVisible(false);
				mySceneNode = AEngine->smgr->getSceneNodeFromName(FB_RIGHT);
				mySceneNode->setScale(core::vector3df(1.0f, 0.1f, 1.0f));
				mySceneNode->setVisible(false);
				mySceneNode = AEngine->smgr->getSceneNodeFromName(FB_UP);
				mySceneNode->setScale(core::vector3df(0.1f, 1.0f, 1.0f));
				mySceneNode->setVisible(false);
				mySceneNode = AEngine->smgr->getSceneNodeFromName(FB_DOWN);
				mySceneNode->setScale(core::vector3df(0.1f, 1.0f, 1.0f));
				mySceneNode->setVisible(false);
				mySceneNode = AEngine->smgr->getSceneNodeFromName(CENTER);
				mySceneNode->setScale(core::vector3df(0.1f, 1.0f, 1.0f));
				mySceneNode->setVisible(false);	
				mySceneNode = AEngine->smgr->getSceneNodeFromName(CUE);
				mySceneNode->setVisible(false);
				cout<<"STOPPING PROTOCOL"<<endl;
				//ProtDealer->pProtocol->MainSound->StopMusic();
				ProtDealer->StopProt();
			}
			break;
		case irr::KEY_KEY_Z:
			mySceneNode = AEngine->smgr->getSceneNodeFromName(FB_LEFT);
			mySceneNode->setScale(core::vector3df(1.0f, 0.1f, 1.0f));
			if (mySceneNode->isVisible()) mySceneNode->setVisible(false); else mySceneNode->setVisible(true); 
			mySceneNode = AEngine->smgr->getSceneNodeFromName(FB_RIGHT);
			mySceneNode->setScale(core::vector3df(1.0f, 0.1f, 1.0f));
			if (mySceneNode->isVisible()) mySceneNode->setVisible(false); else mySceneNode->setVisible(true); 
			mySceneNode = AEngine->smgr->getSceneNodeFromName(FB_UP);
			mySceneNode->setScale(core::vector3df(0.1f, 1.0f, 1.0f));
			if (mySceneNode->isVisible()) mySceneNode->setVisible(false); else mySceneNode->setVisible(true); 
			mySceneNode = AEngine->smgr->getSceneNodeFromName(FB_DOWN);
			mySceneNode->setScale(core::vector3df(0.1f, 1.0f, 1.0f));
			if (mySceneNode->isVisible()) mySceneNode->setVisible(false); else mySceneNode->setVisible(true);  
			mySceneNode = (scene::IParticleSystemSceneNode*)AEngine->smgr->getSceneNodeFromName(FB_LEFT_PAR);
			if (mySceneNode->isVisible()) mySceneNode->setVisible(false); else mySceneNode->setVisible(true); 
			mySceneNode = (scene::IParticleSystemSceneNode*)AEngine->smgr->getSceneNodeFromName(FB_RIGHT_PAR);
			if (mySceneNode->isVisible()) mySceneNode->setVisible(false); else mySceneNode->setVisible(true); 
			mySceneNode = (scene::IParticleSystemSceneNode*)AEngine->smgr->getSceneNodeFromName(FB_UP_PAR);
			if (mySceneNode->isVisible()) mySceneNode->setVisible(false); else mySceneNode->setVisible(true); 
			mySceneNode = (scene::IParticleSystemSceneNode*)AEngine->smgr->getSceneNodeFromName(FB_DOWN_PAR);
			if (mySceneNode->isVisible()) mySceneNode->setVisible(false); else mySceneNode->setVisible(true); 
			break;
		case irr::KEY_KEY_A:
			myStimuli->SetPeriods(&Periods[0],4);
			myStimuli->Start();
			cout<<"LED ON"<<endl;
			break;
		case irr::KEY_KEY_S:
			memset(Periods,0,sizeof(Periods));
			myStimuli->SetPeriods(&Periods[0],4);
			myStimuli->Stop();
			cout<<"LED OFF"<<endl;
			break;
		case irr::KEY_UP:
			KeyMessage = ComposeMessage(CLASSIFICATION,UP);
			ProtDealer->OnSocketEvent(&KeyMessage);
			break;
		case irr::KEY_DOWN:
			KeyMessage = ComposeMessage(CLASSIFICATION,DOWN);
			ProtDealer->OnSocketEvent(&KeyMessage);
			break;
		case irr::KEY_LEFT:
			KeyMessage = ComposeMessage(CLASSIFICATION,LEFT);
			ProtDealer->OnSocketEvent(&KeyMessage);
			break;
		case irr::KEY_RIGHT:
			KeyMessage = ComposeMessage(CLASSIFICATION,RIGHT);
			ProtDealer->OnSocketEvent(&KeyMessage);
			break;
		default:
			ProtDealer->pProtocol->KeyEvent(event);
			break;
		}
	}
	// PROTOCOL EVENTS
	if (event.EventType == irr::EET_USER_EVENT)
	{
		ProtDealer->OnProtocolEvent();
	}
	// SOCKET EVENTS
	if (event.EventType == irr::EET_LOG_TEXT_EVENT)
	{
		if (event.UserEvent.UserData1 == ISBCIMESSAGE)
		{
			BCIMessage *SocketMsg = (BCIMessage*)event.UserEvent.UserData2;
			if(SocketMsg->Check == (SocketMsg->Kind+SocketMsg->Value))
				ProtDealer->OnSocketEvent(SocketMsg);
		}
	}

	if (event.EventType == irr::EET_GUI_EVENT)
	{
		if (ProtDealer->pProtocol != NULL)
			ProtDealer->pProtocol->OnGuiEvent(event);
	}
	return false;
}
